<template>
<div class="login_container">
<div class="login_box">
<!--  这是头像区域-->
<div class="avatar">
  <img src="https://tva1.sinaimg.cn/large/41e64f3aly1gpv0yyog2lj20u00u0e81.jpg" alt=""/>
</div>
<!--  这是登录区域-->
  <el-form label-width="0px" class="login" :model="Loginform" :rules="Loginrules" ref="Loginref">
<!--    用户名-->
  <el-form-item prop="username">
    <el-input v-model="Loginform.username" prefix-icon="iconfont icon-user" ></el-input>
  </el-form-item>
<!--    密码-->
    <el-form-item prop="password">
    <el-input v-model="Loginform.password" prefix-icon="iconfont icon-3702mima" type="password"></el-input>
  </el-form-item>
<!--    按钮-->
        <el-form-item class="btns">
   <el-button type="primary" @click="login">登录</el-button>
           <el-button type="info" @click="reset">重置</el-button>
  </el-form-item>
  </el-form>
</div>
  </div>
</template>

<script>
import {formLogin} from "../../networks/login";

export default {
name: "Login",
  data() {
      return {
        Loginform: {
          username: '',
          password:''
        },
        Loginrules:{
           username: [
            { required: true, message: '夏季学期演示：账号为group1832', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 3到 10 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '夏季学期演示:密码为123456', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 1到 20 个字符', trigger: 'blur' }
          ]

        }
      }
    },
  methods:{
  reset(){
    this.$refs.Loginref.resetFields()
  },
    login(){
    this.$refs.Loginref.validate((valid)=>{
      if(!valid) return console.log("输入有误");
      //发送post请求
      formLogin(this.Loginform.username,this.Loginform.password).then(
          (result)=>{

            console.log(result)
            const {data:res} =result;
            if (result.meta.status!==200){
              this.$message.error("登录信息错误，请重试哦")
            }else {
              this.$message.success("登录成功")
            }
            window.sessionStorage.setItem('token',result.data.token)
            this.$router.push('/home')
          }
      )


    })
    }
  }
}
</script>

<style scoped>
.login_container{
  background: #2b4b6b;
  height: 100%;
}
.login_box{
  background-color: white;
  width: 450px;
  height: 300px;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
}
.avatar{
  width: 130px;
  height: 130px;
  border-radius: 50%;
  border:1px solid #eeeeee;
  padding: 10px;
  box-shadow: 0 0 10px #dddddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%,-50%);
}
.avatar img{
  height: 100%;
  width: 100%;
  border-radius: 50%;
  background-color: #eee;

}
.login{
  position: absolute;
top: 105px;
width: 100%;
padding: 15px;
box-sizing: border-box;
}
.btns{
  display: flex;
  justify-content: right;
}
</style>